package com.powernode.service.impl;

import com.powernode.constant.Constants;
import com.powernode.entity.Goods;
import com.powernode.service.GoodsService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<Goods> queryGoodsList() {
        //存储所有的商品信息
        List<Goods> goodslist = new ArrayList<>();
        //从redis中查询数据，先模糊查询出key
        Set<String> keys = redisTemplate.keys(Constants.REDIS_GOODS + "*");
        keys.forEach(key -> {
            Goods goods = (Goods) redisTemplate.opsForValue().get(key);
            //将随机码设置为null，防止提前暴露
            goods.setRandomName(null);

            goodslist.add(goods);
        });

        return goodslist;
    }


    @Override
    public Goods getById(Integer id) {
        //从redis中查询数据
        String key = Constants.REDIS_GOODS + id;
        Goods goods = (Goods) redisTemplate.opsForValue().get(key);
        //将随机码设置为null，防止提前暴露
        goods.setRandomName(null);
        return goods;
    }


}
